<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */
// ------------------------------------------------------------------------

/**
 * CodeIgniter Application Controller Class
 *
 * This class object is the super class that every library in
 * CodeIgniter will be assigned to.
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/general/controllers.html
 */
class CI_Controller {

    private static $instance;
    protected $_slang_name;
    protected $_serror;
    protected $_ouser;
    public $sesLogName;
    protected $_adata;
    protected $_stemplate;
    public $smessage;
    
    const ADMIN = 1;
    const MEMBER = 2;
    const DEFAULT_TEMPLATE = 'default';
    /**
     * Constructor
     */
    public function __construct() {
        self::$instance = & $this;

        // Assign all the class objects that were instantiated by the
        // bootstrap file (CodeIgniter.php) to local class variables
        // so that CI can run as one big super object.
        foreach (is_loaded() as $var => $class) {
            $this->$var = & load_class($class);
        }

        $this->load = & load_class('Loader', 'core');

        $this->load->initialize();

        $this->_load();

        log_message('debug', "Controller Class Initialized");
    }

    public static function &get_instance() {
        return self::$instance;
    }

    //abstract public function fill_phrase($sview, array $aDataView,array $aphrase = null);
    public function fill_phrase($sview, array $adata_view, array $aphrase = null) {
        //header
        $this->parser->parse_string($this->load->template('header'), $aphrase);
        //left column.
        $this->parser->parse_string($this->load->template('left'), $aphrase);
        //Body.
        $stemplate = $this->load->view($sview, $adata_view, true);
        $this->parser->parse_string($stemplate, $aphrase);
        //Right column.
        $this->parser->parse_string($this->load->template('right'), $aphrase);
        //Footer.
        $this->parser->parse_string($this->load->template('footer'), $aphrase);
    }

    protected function _is_login() {
        if (is_object($this->_ouser)) {
            //Check if time login over 12 hour. If true, destroy session...
            if ((bool) $this->is_over_time_login()) {
                $this->session->unset_userdata($this->sesLogName);
                unset($this->_ouser);
            }
            if ((bool) $this->_ouser) {
                return true;
            }
        }

        return false;
    }

    private function _load() {
        //session_start();
        $this->_slang_name = $this->config->item('language');
        $this->_serror = '';
        //$this->load->library('new_session', null, 'session');
        $this->sesLogName = $this->config->item('data_login');
        $this->load->model('user', '', FALSE, 'user');
        $this->_ouser = $this->session->userdata($this->sesLogName);
        $this->_stemplate = self::DEFAULT_TEMPLATE;
        $this->prevent_ajax();
        //$this->_runHttps();
    }

    //Allow time to be able to continue logining that's 12 hour...
    public function is_over_time_login() {
        if (time() - $this->_ouser->time > 12 * 60 * 60) {
            return true;
        }

        return false;
    }

    /**
     * Redirect to the HTTPS
     *
     * @return void
     */
    protected function _run_https() {
        $isHttps = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on';
        if (!$isHttps) {
            $url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            if ($_SERVER['QUERY_STRING'] > '') {
                $url .= '?' . $_SERVER['QUERY_STRING'];
            }
            header("location: {$url}");
        }
    }
    
    public function return_success_json($var_json, $smessage = '', $others = ''){
//        if(empty($var_json)){
//            return false;
//        }
        
        $ajson = array(
            'result' => 1,
            'message' => $smessage,
            'data' => $var_json
                //'data' => json_encode($var_json)
        );
        
        if(!empty($others)){
            $ajson['other'] = $others;
        }
        
        $str = json_encode($ajson);
        echo $str;
        return $str;
    }
    
    public function return_fail_json($var_json, $smessage = '', $others = ''){
//        if(empty($var_json)){
//            return false;
//        }
        
        $ajson = array(
            'result' => 0,
            'message' => $smessage,
            'data' => $var_json
            //'data' => json_encode($var_json)
        );
        
        if(!empty($others)){
            $ajson['other'] = $others;
        }
        
        $str = json_encode($ajson);
        echo $str;
        return $str;
    }
    
    public function encodeId($iId = 0){
        if(empty($iId)){
            return false;
        }
        
        $this->load->library('encrypt');
        return $this->encrypt->encode($iId, $this->config->item('encode_id'));
        
        return false;
    }
    
    public function decodeId($iId = 0){
        if(empty($iId)){
            return false;
        }
        
        $this->load->library('encrypt');
        return $this->encrypt->decode($iId, $this->config->item('encode_id'));
        
        return false;
    }

    /**
     * Internal redirect funstion.
     *
     * Only redirect to inside this site.
     *
     * @param string $uri
     */
    protected function redirect($uri) {

        $this->load->helper('url');

        $suri_array = explode('/', trim($uri, '/'));
        $uri = '/' . implode('/', $suri_array) . '/';
        $count = count($suri_array);

        if ($count >= 3) {
            redirect($uri);
        } elseif ($count === 2) {
            redirect($this->_module . $uri);
        } elseif ($count === 1) {
            redirect($this->_module . '/' . $this->_controller . $uri);
        }
    }
    
    public function prevent_ajax(){
        if (!(bool) $this->input->is_ajax_request()) {
            $CI = & get_instance();
            if(strpos($CI->router->method, 'ajax_') !== false){
                $this->_redirect_index();
            }
        }
    }
    
    public function __destruct() {
        ob_end_flush();
    }
}

// END Controller class

/* End of file Controller.php */
/* Location: ./system/core/Controller.php */